﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractPcon;
using HamcoWcs.Contract.ContractSrm;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.DbInfos;
using HamcoWcs.Entitys.MainTasks;
using HamcoWcs.Entitys.SubTasks;
using HamcoWcs.Entitys.WcsLocations;
using HamcoWcs.Models.Common;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DbInfos;
using HamcoWcs.Models.DeviceMsgs.SrmMsgs;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.MainTasks;
using HamcoWcs.Models.PlcInfos;
using HamcoWcs.Models.SubTasks;
using HamcoWcs.Service.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HamcoWcs.Service.Srm
{
    [AutoInject(typeof(IBaseSrmTaskService), InjectType.Single)]
    public class SrmBaseService : IBaseSrmTaskService
    {
        private string _plcType = "Srm";
        private Dictionary<string, SrmPlc> _srmDic = new Dictionary<string, SrmPlc>();
        private readonly IMapper _mapper;
        private readonly IServiceScopeFactory _scopeFactory;
        private readonly AutomaticStorageInfo _asrs;

        public SrmBaseService(IMapper mapper, IServiceScopeFactory scopeFactory, AutomaticStorageInfo asrs)
        {
            _asrs = asrs;
            _mapper = mapper;
            _scopeFactory = scopeFactory;
        }



        public SrmPlc GetSrmPlc(string plcName)
        {
            throw new NotImplementedException();
            if (_srmDic.Count <= 0)
            {
                _srmDic = InitSrms();
            }

            //SrmPlc srm;
            if (_srmDic.TryGetValue(plcName, out SrmPlc srm))
            {
                return srm;
            }
            return null;
        }
        public Dictionary<string, SrmPlc> InitSrms()
        {
            throw new NotImplementedException();
            //return _asrs.Srms.ToDictionary(k => k.PlcName, v => v);
            //var srmDic = new Dictionary<string, SrmPlc>();
            //try
            //{
            //    using (var scope = _scopeFactory.CreateScope())
            //    {
            //        var context = scope.ServiceProvider.GetRequiredService<WcsDbContext>();
            //        var srmInfos = context.PlcInfos.Where(x => x.PlcType == _plcType).Include(x => x.DbInfos).AsNoTracking().ToList();
            //        if (srmInfos.Count <= 0) return srmDic;

            //        foreach (var srmInfo in srmInfos)
            //        {
            //            var srm = new SrmPlc(srmInfo.PlcName);
            //            srm.SrmPlcInfo = _mapper.Map<PlcInfoDto>(srmInfo);
            //            srm.SrmDbInfo = _mapper.Map<IEnumerable<DbInfoDto>>(srmInfo.DbInfos).ToList();

            //            //var aisleName = (srm.PlcName == "Srm01" || srm.PlcName == "Srm02") ? "CJSTW100" : "CJSTW200";

            //            var node = _asrs.Nodes.FirstOrDefault(x => x.NodeName.Equals(srm.MachineName, StringComparison.OrdinalIgnoreCase));

            //            node.Srms.Add(srm);
            //            srm.Aisle = node;

            //            //conDto.Machine = node;
            //            //srm.Aisle = _asrs.Nodes.FirstOrDefault(x => x.NodeName.Equals(aisleName, StringComparison.OrdinalIgnoreCase));
            //            srmDic.TryAdd(srmInfo.PlcName, srm);
            //        }
            //        return srmDic;
            //    }
            //}
            //catch (Exception e)
            //{
            //    return null;
            //}
            //finally
            //{
            //    //_isCompleted = true;
            //}

        }

    }

}
